﻿using System.ComponentModel;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.WebApi.ServicesStub;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Kingdee.BOS.Log;
using Kingdee.BOS;

namespace Hamster.K3Cloud.WebAPI.ServerExtend
{
    [Description("Hamster WebAPI 接口"), HotUpdate]
    public class BusinessService : AbstractWebApiBusinessService
    {
        public BusinessService(KDServiceContext context) : base(context) { }

        /// <summary>
        /// 执行接口请求
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public Result ExcuteService(string parameters)
        {
            parameters = parameters.Trim();
            Logger.Error("=======ExcuteService Client Origin Parameters=======", parameters, null);
            Result result = Checker.clientParameterChecker(parameters);
            if (!result.success)
            {
                return result;
            }

            return ServiceRouter(parameters);
        }

        /// <summary>
        /// 接口请求分发
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private Result ServiceRouter(string parameters)
        {
            Parameter parameter = JsonConvert.DeserializeObject<Parameter>(parameters);
            parameter = Checker.parameterHandler(parameter);
            Result checkRs = Checker.checkModel(parameter); // 业务参数不能为空
            if (!checkRs.success)
            {
                return checkRs;
            }

            Result result = new Result();
            Context context = KDContext.Session.AppContext;

            // 参数再次校验
            Result resultCheck = Checker.checkParameter(parameter);
            if (!resultCheck.success)
            {
                return resultCheck;
            }

            // 单据处理动作分发
            if (parameter.ActionName == "Push") {                   // 下推
                result = BillHandler.Push(context, parameter);
            } else if (parameter.ActionName == "Save") {            // 保存
                result = BillHandler.Save(context, parameter);
            } else if (parameter.ActionName == "Delete") {          // 删除
                result = BillHandler.Delete(context, parameter);
            } else if (parameter.ActionName == "Audit") {           // 审核
                result = BillHandler.Audit(context, parameter);
            } else if (parameter.ActionName == "UnAudit") {         // 反审核
                result = BillHandler.UnAudit(context, parameter);
            } else if (parameter.ActionName == "PushAndUpdate") {   // 下推更新
                result = BillHandler.PushAndUpdate(context, parameter);
            }

            return result;
        }
    }
}
